<!-- 商品列表 -->
<template>
   <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图 -->
        <el-card>
            <el-row>
                <!-- 搜索输入框 -->
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryParams.query" clearable @clear="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <!-- 添加商品按钮 -->
                <el-col :span="4">
                    <el-button type="primary" @click="toAddPage" class="addGoodsButton">添加商品</el-button>
                </el-col>
                <!-- 商品列表 -->
                <el-table
                    :data="goodsList"
                    border
                    stripe
                    style="width: 100%">
                    <el-table-column type="index" label="#" ></el-table-column>
                    <el-table-column prop="goods_name" label="商品名称" ></el-table-column>
                    <el-table-column prop="goods_price" label="商品价格(元)" width="93px"></el-table-column>
                    <el-table-column prop="goods_weight" label="商品重量" width="73px"></el-table-column>
                    <el-table-column prop="add_time" label="创建时间" width="150px"></el-table-column>
                    <el-table-column label="操作" width="120px">
                        <template slot-scope="scope">
                            <!-- 修改 -->
                            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showModifyGoodsDialog(scope.row.attr_id)"></el-button>
                            <!-- 删除 -->
                            <template>
                                <el-button type="danger" icon="el-icon-delete" size="mini" @click="confirmDelete(scope.row.goods_id)"></el-button>
                            </template>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryParams.pagenum"
                    :page-sizes="[10, 15, 20]"
                    :page-size="queryParams.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="this.total">
                </el-pagination>
            </el-row>
        </el-card>
   </div>
</template>

<script>
    export default {
        data () {
            return {
                // 商品数据
                goodsList: [],
                // 查询商品列表的参数
                queryParams: {
                    query: '',
                    pagenum: 1,
                    pagesize: 10
                },
                // 总商品数
                total: 0,
                
          };
        },
        created() {
            this.getGoodsList()
        },
        methods: {
            // 获取商品数据
            getGoodsList: async function() {
                const{data: res} = await this.$http.get('goods', {params: this.queryParams})
                console.log('getGoodsList: ',res);
                if(res.meta.status !== 200) {
                    return this.$message.error('查询商品列表失败！')
                }
                res.data.goods.forEach(item => {
                    item.add_time = this.getLocalTime(item.add_time)
                })
                this.goodsList = res.data.goods
                this.total = res.data.total
                
            },
            // 时间戳 -> 格式化日期
            getLocalTime: function(nS) {     
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
            },
            // 跳转到添加商品的页面
            toAddPage: function() {
                this.$router.push('/goods/add')
            },
            // 展示编辑商品的对话框
            showModifyGoodsDialog: function(id) {

            },
            // 确认删除商品
            confirmDelete: async function(id) {
                const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch( () => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    }); 
                })
                if(confirmResult === 'confirm') {
                    const {data: res} = await this.$http.delete(`goods/${id}`)
                    if(res.meta.status !== 200) {
                        return this.$message.error('删除失败!')
                    }
                    this.getGoodsList()
                    this.$message.success('删除成功!')
                }
            },
            // 改变一页显示数量
            handleSizeChange: function(newSize) {
                console.log('page-size: ',newSize);
                this.queryParams.pagesize = newSize
                this.getGoodsList()
            },
            // 点击页数跳转
            handleCurrentChange: function(newPage) {
                console.log('newPage', newPage);
                this.queryParams.pagenum = newPage
                this.getGoodsList()
            }
        },
        computed: {
            
        }
    }

</script>
<style lang='less' scoped>
.addGoodsButton {
    margin-left: 15px;
}
.el-input {
    margin-bottom: 15px;
}
</style>